How to Use SetPixel Method to Draw a Function in VB.NET

This article shows how to use the SetPixel method to draw some mathematical functions. I created the MathGraph project under VB.NET (2003) to draw the following functions:



1- f(X) = x^2
2- f(X) = X^3
3- f(X) = sin(X)
4- f(X) = cos(X)
5- f(X) = |X^2- 4|
6- f(X) = |sin(X)|

The MathGraph project has one form (frmGraph) with the following controls:

  • ListBox control (lstFunction) to select a function.
  • Label control (lblFunctionName) to display function definition.
  • PictureBox control (PicView) to display the function graph, this control has a background image from the file (..\..\images\Axis.jpg).
  • Button control (btnDraw) to draw the function graph.
  • Button control (btnExit) to close the program.

The Code

  1. ' Variables of my program  
  2. Dim PointArray As ArrayList = New ArrayList   ' series of points  
  3. Dim PageWidth As Integer ' Width of "ImageToDraw" Object  
  4. Dim PageHeight As Integer  ' Height of "ImageToDraw" Object  
  5. Dim xCenter As Integer  ' x coordinate of center  
  6. Dim yCenter As Integer  ' x coordinate of center  
  7. Dim ImageToDraw As Bitmap  
  8. Dim Pi As Double = Math.PI ' 22/7  
  9.   
  10. //Set the Center coordinates and load ListBox with Functions:  
  11. Private Sub frmGraph_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Load  
  12.         ' Width of "PicView" control  
  13.         PicView.Width = 500 'don't change (it is for background image  
  14.         ' Height of "PicView" control  
  15.         PicView.Height = 380 'don't change (it is for background image  
  16.   
  17.         ' Width of "ImageToDraw" Object as Width of  "PicView" control  
  18.         PageWidth = PicView.Width  
  19.         xCenter = Int(PageWidth / 2)  
  20.         ' Height of "ImageToDraw" Object as Height of  "PicView" control  
  21.         PageHeight = PicView.Height  
  22.         yCenter = Int(PageHeight / 2)  
  23.   
  24.         ' Fill ListBox  
  25.         lstFunction.Items.Add("Function 1")  
  26.         lstFunction.Items.Add("Function 2")  
  27.         lstFunction.Items.Add("Function 3")  
  28.         lstFunction.Items.Add("Function 4")  
  29.         lstFunction.Items.Add("Function 5")  
  30.         lstFunction.Items.Add("Function 6")  
  31.         lstFunction.SelectedIndex = 0  
  32. End Sub  
  33.   
  34. // Draw the function: f(X) = |Sine(X)|  
  35. Private Sub Function6()  
  36.         Dim W As Double = PageWidth / 20  
  37.         Dim H As Double = PageHeight / 4  
  38.         ImageToDraw = New Bitmap(PageWidth, PageHeight)  
  39.         ' Draw the function f(X) = |Sine(X)|.  
  40.         For X As Double = -2 * Pi To 2 * Pi Step 0.01  
  41.             Dim Z As Double = Math.Sin(X)  
  42.             Dim Y As Double = Math.Abs(Z)  
  43.             Dim PX As Double = xCenter + W * X  
  44.             Dim PY As Double = yCenter - H * Y  
  45.             ImageToDraw.SetPixel(Int(PX), Int(PY), Color.Blue)  
  46.         Next  
  47.         PicView.Image = ImageToDraw  
  48.         ImageToDraw = Nothing  
  49.     End Sub 

Summary

In this article we have learned how to use the SetPixel method to draw curves. You can go to the source code file to read the code for drawing the other functions. If you have any idea about this code, please tell me. Thanks for C# Corner team and thanks for all.

Up Next
    Ebook Download
    View all
    Learn
    View all